// pages/servicelIst/index.js
const tabService = require("../../utils/tab-service");
const app = getApp()
let {
  baseUrl,
  commonUpdate,
  commonUpload
} = app.globalData
import {
  navigationTop,
  navigationBottom
} from "./data.js"
import {
  ApiGetSpecialList,
  ApiCategoryList,
  getCustomerPhone,
  ApiWaitReceiveCouponList,
} from '../../api/index'

// const app = getApp()
// let {
//   baseUrl,
//   commonUpdate,
//   commonUpload
// } = app.globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl,
    commonUpdate,
    commonUpload,
    isNotice: false,
    active: 1,
    navigationTop,
    params: {
      search_data: ''
    },
    specialArr: [],
    maternalArr: [],
    categoryArr: []
  },

  onLoad() {
    this.getCategoryList()
    this.getSpecialistCare()

  },

  getUserInfo() {
    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
    }, () => {

    })
  },
  openMembership() {
    if (this.data.userInfo.card_no) {
      wx.navigateTo({
        url: '/pages/hospitalChoice/index',
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '请先实名认证',
        complete: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: `/pages/realName/index?data=${JSON.stringify(this.data.userInfo)}`,
            })
          }
        }
      })
    }

  },
  //获取所有类别数据
  getCategoryList() {
    ApiCategoryList().then(res => {
      this.setData({
        categoryArr: res.data
      })
    })
  },

  getWaitlist() {
    const token = wx.getStorageSync('token')
    if (token) {
      ApiWaitReceiveCouponList().then(res => {
        let val = res.data.find((item) => {
          return item.status == '可领取'
        })
        if (val) {
          this.setData({
            isNotice: true
          })
        } else {
          this.setData({
            isNotice: false
          })
        }
      })
    }
  },

  onShow() {
    this.getUserInfo()
    this.getWaitlist()
    if (typeof this.getTabBar === "function" &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onSearch(e) {
    console.log(e)
    wx.navigateTo({
      url: `/pages/searchApp/index?data=${e.detail}`,
    })
  },


  handleGetPhone(e) {
    const that = this
    wx.showModal({
      title: "是否确认拨打电话？",
      success(res) {
        if (res.confirm) {
          getCustomerPhone().then(res => {
            that.onSelect(res.data)
          })

        }
      }

    })
    /*    const tel = '123456789'
       this.setData({
         showPhone: true,
         'actions[0].name': `${tel}`
       }) */
  },
  //拨号成功
  onSelect(event) {
    const tel = event
    wx.makePhoneCall({
      phoneNumber: tel,
      success: function () {
        console.log("拨号成功！")
      },
      fail: function () {
        console.log("拨号失败！")
      }
    })
  },

  onClickLookMore(e) {
    let allPath = JSON.stringify(this.data.categoryArr)
    wx.navigateTo({
      url: `/pages/orderSpecialAll/index?detail=${e.currentTarget.dataset.v}&allPath=${allPath}`,
    })
  },

  //专科护理
  getSpecialistCare() {
    ApiGetSpecialList().then(res => {
      res.data.forEach(item => {
        item.inthroLabel = item.intro.slice(0, 10)
      })
      this.setData({
        specialArr: res.data.splice(0, 4)
      })
    })
  },
  onChange(event) {
    wx.showToast({
      title: `切换到标签 ${event.detail.name}`,
      icon: 'none',
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})